package com.pj.ca.service.impl;

import com.pj.ca.service.AsymmetricalSignature;
import com.pj.ca.util.AsymmetricalUtil;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

/**
 * 描述：DSA 仅支持签名
 *
 * @author pengjun
 * @date 2021/11/3 11:16
 */
public class DSA implements AsymmetricalSignature {

    @Override
    public byte[] sign(byte[] data, PrivateKey privateKey, String signAlgorithm) throws Exception {
        Signature signature = AsymmetricalUtil.getSignatureInstance(signAlgorithm);
        signature.initSign(privateKey);
        signature.update(data);
        return signature.sign();
    }

    @Override
    public boolean verifySign(byte[] signData, byte[] content, PublicKey publicKey, String signAlgorithm) throws Exception {
        Signature signature = AsymmetricalUtil.getSignatureInstance(signAlgorithm);
        signature.initVerify(publicKey);
        signature.update(content);
        return signature.verify(signData);
    }
}
